<?php
/**
 * @package DBpretty
 * @author WiFeng
 * @link http://521-wf.com
 */

if(!defined('__TYPECHO_ROOT_DIR__')) {
	exit('Access Denied');
};
$user->pass('administrator', false);

require_once './header.php';
require_once './menu.php';

if(!isset($request->operation) || !in_array($request->operation, array('export', 'import', 'optimize'))) {
	$request->setParam('operation', 'export');
}

?>

<div class="main">
    <div class="body body-950">
        <?php include './page-title.php'; ?>
        <div class="container typecho-page-main">
        	<div class="column-24">
        		<ul class="typecho-option-tabs">
	                <li<?php if('export' == $request->get('operation')): ?> class="current"<?php endif; ?>><a href="<?php $options->adminUrl('extending.php?panel=DBpretty/manage.php&operation=export'); ?>"><?php _e('备份'); ?></a></li>
	                <li<?php if('import' == $request->get('operation')): ?> class="current"<?php endif; ?>><a href="<?php $options->adminUrl('extending.php?panel=DBpretty/manage.php&operation=import&do=showfiles'); ?>"><?php _e('恢复'); ?></a></li>
	                <!-- <li<?php if('optimize' == $request->get('operation')): ?> class="current"<?php endif; ?>><a href="<?php $options->adminUrl('extending.php?panel=DBpretty/manage.php&operation=optimize'); ?>"><?php _e('优化'); ?></a></li> -->
                </ul>
        	</div>
			<?php if('export' == $request->get('operation')): ?>
			<div class="column-24">
			<?php Typecho_Widget::widget('DBpretty_Options_Export')->form()->render(); ?>
			</div>
			<?php elseif('import' == $request->get('operation') && ('showfiles' == $request->get('do') || '' == trim($request->get('do')))): 
			$backupFiles = Typecho_Widget::widget('DBpretty_Options_Import')->getFiles();
			$backupDirUrl = Typecho_Widget::widget('DBpretty_Do')->getBackupDirUrl();
			?>
        	<div class="column-24">
        		<?php if(!empty($backupFiles)): ?>
				<form method="post" name="manage_posts" class="operate-form" action="<?php $options->index('/action/dbpretty-do'); ?>">
        		<div class="typecho-list-operate">
				
						<p class="operate">操作: 
							<span class="operate-button typecho-table-select-all">全选</span>, 
							<span class="operate-button typecho-table-select-none">不选</span>&nbsp;&nbsp;&nbsp;
							选中项: 
							<span rel="delete" lang="你确认要删除这些备份文件吗?" class="operate-button operate-delete typecho-table-select-submit">删除</span>
						</p>
					
                </div>
				
                <table class="typecho-list-table">
                    <colgroup>
						<col width="15"/>
                        <col width="25"/>
                        <col width="150"/>
                        <col width="120"/>
                        <col width="80"/>
                        <col width="180"/>
                      
                    </colgroup>
                    
                    <tbody>
                    	<?php 
						$key = 0;
						foreach($backupFiles as $file): 
							$key ++ ;
						?>
                    	<tr>
							<td><?php echo $key; ?></td>
							<td><input type="checkbox" value="<?php echo $file['name']; ?>" name="filename[]"/></td>	
							<td><?php echo _t('[文件名] ').urldecode($file['name']); ?></td>
							<td><?php echo _t('[备份时间] ').$file['filemtime']; ?></td>
							<td><?php echo _t('[大小] ').$file['size']; ?></td>
							<td>
								<a href="javascript:void(0)" rel="<?php echo $file['name']; ?>" onclick="execFile(this)">
								<?php echo _t('导入'); ?>
								</a>&nbsp;&nbsp;&nbsp;&nbsp;
								<a href="<?php echo $backupDirUrl.urlencode($file['name']); ?>">
								<?php echo _t('保存'); ?>
								</a>
							</td>
						</tr>
                    	<?php endforeach; ?>
                    </tbody>
                </table>
                <input type="hidden" name="do" value="delete" />
				<input type="hidden" name="importfilename" value="" />
				<input type="hidden" name="operation" value="delete" />
                </form>
                <?php else: ?>
                <div>还没有备份记录哦，赶快去备份吧</div>
                <?php endif; ?>
				
        	</div>
        	<?php endif; ?>
        </div>
    </div>
</div>

<?php
require_once './copyright.php';
require_once './common-js.php';
?>


<?php if('export' == $request->get('operation')): ?>
<script type="text/javascript">
window.addEvent('domready', function() {
    $$("form input[name=type]").addEvent("click", function() {
    	$$(this).each(function(input) {
            var allNext = input.getParent('li').getAllNext();
    		if(input.id == "type-all") {
    			$$("form #tablename-selectall, form input[name='tablename[]']").setProperty("checked", "checked");
    			allNext.setProperty("style", "display:none");
    		} else {
    			allNext.setProperty("style", "display:block");
    		}	
        });
	});
		
	$$("form #tablename-selectall").addEvent("click", function() {
		var checked = $$(this).getProperty("checked");
		if(checked.toString() == "false") {
			$$("form input[name='tablename[]']").removeProperty("checked");
		} else {
			$$("form input[name='tablename[]']").setProperty("checked", "checked");
		}
	});

	$$("form input[name='tablename[]']").addEvent("click", function() {
		$$("form #tablename-selectall").removeProperty("checked");
	});	
	
});

function exportDB(obj) {
	var action = $$(obj).getParent("form").getProperty("action");
	var type = $$("form input[name=type]:checked").getProperty("value");
	var tablenames = $$("form input[name='tablename[]']:checked").getProperty("value");
	var sqlcompat = $$("form input[name=sqlcompat]:checked").getProperty("value");
	var usezip = $$("form input[name=usezip]:checked").getProperty("value");
	var filename = $$("form input[name=filename]").getProperty("value");
	var operation = "export";

	$$("#dbpretty-export-msg").getPrevious("input").setProperty("value", "正在备份").setProperty("disabled", true);
	$$("#dbpretty-export-msg").set("html", "请不要离开此页面  ......");
	var myRequest = new Request.JSON({
	    method: "post", 
	    url: action
	});
	myRequest.send("action="+action+"&type="+type+"&tablenames="+tablenames+"&sqlcompat="+sqlcompat+"&usezip="+usezip+"&filename="+filename+"&operation="+operation);
	myRequest.onSuccess = function(responseText, responseXML) {
		if(responseText && typeof responseText == "object") {
			var msg = responseText.msg;
			var status = responseText.status;
			if(status == "ok") {
				window.location.href = "<?php $options->adminUrl('extending.php?panel=DBpretty/manage.php&operation=import'); ?>";
			} else {
				alert(msg);
			}
		}

		$$("#dbpretty-export-msg").set("html", "");
		$$("#dbpretty-export-msg").getPrevious("input").setProperty("value", "开始备份").setProperty("disabled", false);
	}
}
</script>
<?php elseif('import' == $request->get('operation')): ?>
<script type="text/javascript">
function execFile(thisObj) {
	$$(thisObj).removeProperty("target");
	var filename = $$(thisObj).get('rel');

	if(!confirm('您真的要导入吗？导入操作会覆盖原始数据。')) {
		return false;
	}
	
	$$("form.operate-form input[name=importfilename]").set('value', filename);
	$$("form.operate-form input[name=operation]").set('value', 'exec');
	$$("form.operate-form")[0].submit();

}
</script>
<?php endif; ?>

<?php
require_once './footer.php'; 
?>

